/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ie.tcd.cs.railtickets.server.dao.impl;

import ie.tcd.cs.railtickets.server.dao.BaseDAO;
import static ie.tcd.cs.railtickets.server.dao.impl.HibernateSessionFactory.getSession;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Wenbo Sun
 */
public abstract class AbstractBaseDAOImpl implements BaseDAO {

    private static final Log log = LogFactory.getLog(AbstractBaseDAOImpl.class);
    
    public void delete(Object entity) {
        try {
            getSession().delete(entity);
            log.debug("object has been deleted!");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    public void save(Object entity) {
        try {
            getSession().save(entity);
            log.debug("object has been saved!");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    public void update(Object entity) {
        try {
            getSession().update(entity);
            log.debug("object has been updated!");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public List findAll(Class clazz) {
        String query = "from " + clazz.getSimpleName();
        List result = getSession().createQuery(query).list();
        return result;

    }

    public Object findById(Class clazz, long id) {
        Object result = getSession().load(clazz, id);
        return result;
    }



}
